<?php

namespace Biz\service\Album;

use Biz\model\Album\AlbumListModel;
use Doraemon\cache\AlbumCounter;
use Doraemon\model\Favorite as mFavorite;
use Doraemon\service\Favorite;
use Doraemon\service\Like;
use Doraemon\service\User;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient as tHhzClient;
use Doraemon\tools\Hhzurl;


class FormatAlbumService
{

    const SELF_BUILT_LIST = 'self_built_list'; //我的清单页-自建清单列表
    const DRAWER_ALBUM_LIST = 'drawer_album_list'; //商品添加至清单的抽屉页面展示清单列表
    const SELF_FAV_LIST = 'self_fav_list'; //我的清单页-收藏清单列表
    const SQUARE_LIST = 'square_list'; //清单广场
    const MASONRY_LIST = 'masonry_list'; //双列卡片
    const WIKI_FEED_LIST = 'wiki_feed_list'; //好物频道feed流插入清单卡片



    /**
     * @param $albumInfos
     * @param string $filedGroup
     * @return array
     */
    public static function _formatAlbumList($albumInfos, string $filedGroup = self::SELF_BUILT_LIST): array
    {
        $result = [];
        if ($albumInfos && is_array($albumInfos)) {
            $albumIds = array_column($albumInfos, 'album_id');
            $albumCounter = AlbumCounter::getMulti($albumIds);
            $userInfoMap = [];
            $likesMap = [];
            $favoriteMap = [];
            switch ($filedGroup) {
                case self::SELF_FAV_LIST:
                case self::MASONRY_LIST:
                case self::WIKI_FEED_LIST:
                    $uidArr = array_column($albumInfos, 'uid');
                    $userInfoMap = User::getBatchBasicUserInfo($uidArr);
                    $userInfoMap = array_column($userInfoMap, null, 'uid');

                    //点赞信息
                    $likesMap = Like::isLikedMulti(tHhzClient::getUid(), $albumIds);
                    // 收藏信息
                    $favoriteMap = Favorite::isFavoritedMulti(
                        tHhzClient::getUid(),
                        $albumIds,
                        mFavorite::TYPE_WIKI_ALBUM
                    );
                    break;
            }
            foreach ($albumInfos as $val) {
                $info = [
                    'album_id' => $val['album_id'],
                    'title' => $val['title'],
                    'desc' => $val['desc'],
                    'uid' => $val['uid'],
                    'status' => $val['status'],
                    'wiki_count' => $val['wiki_count'],
                    'joiner_num' => $albumCounter[$val['album_id']]['read'] ?? 0,
                    'cover_img_url' => $val['cover_img'],
                    'quality' => $val['quality'],
                    'album_link' => Hhzurl::getAlbumDetailNiceLivingLink($val['album_id']),
                ];
                switch ($filedGroup) {
                    case self::SELF_FAV_LIST:
                    case self::MASONRY_LIST:
                    case self::WIKI_FEED_LIST:
                        $info['user_info'] = $userInfoMap[$val['uid']] ?? (object)[];
                        //计数器数据
                        $info['counter'] = [
                            'like' => $albumCounter[$val['album_id']]['like'] ?? 0,
                            'comments' => $albumCounter[$val['album_id']]['comment'] ?? 0,
                            'favorite' => $albumCounter[$val['album_id']]['favorite'] ?? 0,
                        ];
                        $info['is_like'] = $likesMap[$val['album_id']] ?? 0; //是否点赞了
                        $info['is_liked'] = $likesMap[$val['album_id']] ?? 0; //冗余字段，用于统一客户端使用
                        $info['is_favorite'] = $favoriteMap[$val['album_id']] ?? 0; //是否收藏了
                        break;
                    case self::SELF_BUILT_LIST:
                    case self::DRAWER_ALBUM_LIST:
                        break;
                    case  self::SQUARE_LIST:
                        $info['is_official'] = in_array($val['uid'], AlbumListModel::DEFAULT_UID_HAOHAOZHU) ? 1 : 0;
                        break;
                    default:
                        break;
                }
                $result[] = [
                    'type' => BlockType::TYPE_WIKI_ALBUM,
                    'album_info' => $info,
                ];
            }
        }

        return $result;
    }

}
